import datetime
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# #编写自动化脚本，具体步骤
# #1.打开浏览器----驱动管理
# ChromeIns=ChromeDriverManager().install()
# ###创建谷歌浏览器驱动对象
# driver=webdriver.Chrome(service=Service(ChromeIns))
# time.sleep(2)
# #2.输入百度网址：https://www.baidu.com（网址一定要完整，不要写成www.baidu.com)
# driver.get("https://www.baidu.com")
# time.sleep(2)
# #3.找到百度输入框，输入关键词“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# time.sleep(2)
# #4.点击“百度一下”按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)
# #5.关闭浏览器
# driver.quit()

#1.打开浏览器----驱动管理
ChromeIns=ChromeDriverManager().install()
#浏览器参数配置
options = webdriver.ChromeOptions()
#添加无头模式
# options.add_argument("-headless")
#添加页面加载策略
# options.page_load_strategy = 'normal'
# options.page_load_strategy = 'normal'
options.page_load_strategy = 'none'
###创建谷歌浏览器驱动对象
driver=webdriver.Chrome(service=Service(ChromeIns), options=options)
driver.get("https://haokan.baidu.com/")
print(driver.title)
# ret=driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)# 获取每个元素的文本信息

# 点击事件
# 查找元素+点击元素
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# ele=driver.find_element(By.CSS_SELECTOR, "#su")
# ele.click()

# driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(3) > a").click()

#查找元素+输入文本
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("周杰伦")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#kw").clear()
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("林俊杰")

#获取文本信息
# text=driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title").text
# print(text)

#获取百度一下按钮上的文字
# text = driver.find_element(By.CSS_SELECTOR, "#su").text
# text = driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print(text)
# assert text == "百度一下"

# 获取页面标题和URL
# title = driver.title
# url = driver.current_url
#
# print(title)
# print(url)


#点击百度首页的图片功能，转跳到百度图片页面
#测试跳转结果
# print("befor: " + driver.title)
# print("befor: " + driver.current_url)
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# #获取当前页面的句柄----第一个标签页
# curHandle = driver.current_window_handle
# #获取所有句柄
# allHandle = driver.window_handles
# #遍历所有的句柄，切换到新的页面
# for handle in allHandle:
#     if handle != curHandle:
#         #切换句柄
#         #driver.switch_to_window(handle)
#         driver.switch_to.window(handle )
# #测试跳转结果
# print("after: " + driver.title)
# print("after: " + driver.current_url)


# #设置窗口大小
# #最大化
# driver.maximize_window()
# time.sleep(2)
# #最小化
# driver.minimize_window()
# time.sleep(2)
# #全屏
# driver.fullscreen_window()
# time.sleep(2)
# #手动设置尺寸
# driver.set_window_size(1024, 800)
# time.sleep(2)



# #添加屏幕截图
# # filename = "./images/test.png"
# filename = "autoTest-" + datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + '.png'
# driver.save_screenshot('./images/' + filename)


#打开新的标签页
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# time.sleep(3)
# driver.close()
# time.sleep(3)


# driver.implicitly_wait(3)
# driver.get("https://www.baidu.com")

# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
#
# #添加显示等待
# #创建显示等待类对象
# wait = WebDriverWait(driver, 3)
# wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")))
# # driver.find_element(By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")


# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# time.sleep(2)
#
# #后退
# driver.back()
# time.sleep(2)
# #前进
# driver.forward()
# time.sleep(2)
# #刷新
# driver.refresh()
# time.sleep(2)

driver.quit()



